package cn.com.duiba.millionaire.center.biz.service.impl;

import cn.com.duiba.millionaire.center.api.constant.RedisKeyFactory;
import cn.com.duiba.millionaire.center.api.dto.MillionaireActivityDto;
import cn.com.duiba.millionaire.center.api.dto.MillionaireMainMeetingDto;
import cn.com.duiba.millionaire.center.api.requestParam.MillionaireMainMeetingRequestParam;
import cn.com.duiba.millionaire.center.biz.dao.MillionaireActivityDao;
import cn.com.duiba.millionaire.center.biz.dao.MillionaireMainMeetingDao;
import cn.com.duiba.millionaire.center.biz.dao.MillionaireMtAcRelationDao;
import cn.com.duiba.millionaire.center.biz.entity.MillionaireActivityEntity;
import cn.com.duiba.millionaire.center.biz.entity.MillionaireMainMeetingEntity;
import cn.com.duiba.millionaire.center.biz.entity.MillionaireMtAcRelationEntity;
import cn.com.duiba.millionaire.center.biz.service.MillionaireMainMeetingService;
import cn.com.duiba.wolf.utils.BeanUtils;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * Created by sty on 1/20/18.
 */
@Service
public class MillionaireMainMeetingServiceImpl implements MillionaireMainMeetingService {

    @Autowired
    private MillionaireMainMeetingDao millionaireMainMeetingDao;
    @Autowired
    private MillionaireMtAcRelationDao millionaireMtAcRelationDao;
    @Autowired
    private MillionaireActivityDao millionaireActivityDao;

    @Resource(name="redisTemplate")
    private RedisTemplate<String,MillionaireMainMeetingDto> redisTemplate;

    private String buildRedisKey(Long meetingId){
        return RedisKeyFactory.K010.toString()+meetingId;
    }

    @Override
    public Integer countMillionaireMainMeetingList(MillionaireMainMeetingRequestParam param) {
        return millionaireMainMeetingDao.findByPageCount();
    }

    @Override
    public List<MillionaireMainMeetingDto> getMillionaireMainMeetingList(MillionaireMainMeetingRequestParam param) {
        return BeanUtils.copyList(millionaireMainMeetingDao.findByPage(param.getPageNum(), param.getPageSize()), MillionaireMainMeetingDto.class);
    }

    @Override
    public MillionaireMainMeetingDto findMillionaireMeetingById(Long id) {
        MillionaireMainMeetingDto redisMainMeeting = redisTemplate.opsForValue().get(buildRedisKey(id));
        if(redisMainMeeting!=null){
            return redisMainMeeting;
        }

        MillionaireMainMeetingDto one = BeanUtils.copy(millionaireMainMeetingDao.findById(id), MillionaireMainMeetingDto.class);
        if(one == null){
            return null;
        }
        one.setActivityList(Lists.newArrayList());
        //查找分会场活动信息
        List<MillionaireMtAcRelationEntity> activityIds = millionaireMtAcRelationDao.findByMainMeetingId(id);
        if(CollectionUtils.isEmpty(activityIds)){
            redisTemplate.opsForValue().set(buildRedisKey(id), one, 1, TimeUnit.MINUTES);
            return one;
        }
        List<MillionaireActivityEntity> activitys = millionaireActivityDao.findByIds(activityIds.stream().map(MillionaireMtAcRelationEntity::getActivityId).collect(Collectors.toList()));
        activitys = activitys.stream().filter(activity -> Objects.equals(MillionaireActivityDto.STATUS_OPEN, activity.getOpenStatus())).collect(Collectors.toList());

        if(CollectionUtils.isEmpty(activitys)){
            redisTemplate.opsForValue().set(buildRedisKey(id), one, 1, TimeUnit.MINUTES);
            return one;
        }
        one.setActivityList(BeanUtils.copyList(activitys, MillionaireActivityDto.class));
        redisTemplate.opsForValue().set(buildRedisKey(id), one, 1, TimeUnit.MINUTES);
        return one;
    }

    @Override
    public Boolean delete(Long id) {
        boolean result =  millionaireMainMeetingDao.deleteById(id)>0;
        //绑定关系删除
        if(result){
            redisTemplate.delete(buildRedisKey(id));
            millionaireMtAcRelationDao.deleteByMainMeetingId(id);
        }
        return result;
    }

    @Override
    public Boolean saveOrUpdateMillionaireMainMeeting(MillionaireMainMeetingDto dto, List<Long> activityIds) {
        if(dto.getId() == null){
            dto = BeanUtils.copy(millionaireMainMeetingDao.insert(BeanUtils.copy(dto, MillionaireMainMeetingEntity.class)), MillionaireMainMeetingDto.class);
            if(dto ==null || dto.getId()==null){
                return false;
            }
            //更新绑定关系表
            if(CollectionUtils.isNotEmpty(activityIds)) {
                millionaireMtAcRelationDao.batchInsert(dto.getId(), activityIds);
            }

        }else{
            int ret  = millionaireMainMeetingDao.updateById(BeanUtils.copy(dto, MillionaireMainMeetingEntity.class));

            if(ret <=0 ){
                return false;
            }
            millionaireMtAcRelationDao.deleteByMainMeetingId(dto.getId());
            if(CollectionUtils.isNotEmpty(activityIds)) {
                millionaireMtAcRelationDao.batchInsert(dto.getId(), activityIds);
            }
        }

        redisTemplate.delete(buildRedisKey(dto.getId()));
        return true;
    }

    @Override
    public List<MillionaireMainMeetingEntity> findByIds(List<Long> ids) {
        return millionaireMainMeetingDao.findByIds(ids);
    }


}
